package com.etrans.web.gps.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.XMLType;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class WebServiceUtil {
	
	private final static String urlStr = "http://180.139.137.118:6012/OVEInterface/OVEInterface.asmx";
	
	private final static String namespace =  "http://tempuri.org/";
	
	@SuppressWarnings("rawtypes")
	public static void getCarInfo(String cphm, String cpys,String yszhm,String ziparea) {
        String methodName = "GetCarInfo";
        String soapActionURI = namespace+methodName;
        Service service = new Service();
        Call call;
        try {
            call = (Call) service.createCall();
            call.setTargetEndpointAddress(urlStr);
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(soapActionURI);
            call.setOperationName(new QName(namespace, methodName));
            call.addParameter(new QName(namespace, "CPHM"), XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter(new QName(namespace, "CPYS"), XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter(new QName(namespace, "YSZHM"), XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter(new QName(namespace, "ziparea"), XMLType.XSD_STRING,ParameterMode.IN);
            call.setReturnType(new QName(namespace, methodName), List.class); // 返回的数据类型
            Object[] str = new Object[4];
            str[0] = cphm;
            str[1] = cpys;
            str[2] = yszhm;
            str[3] = ziparea;
            List v = (List) call.invoke(str); 
		    for (int i = 0; i < v.size(); i++) { 
		    	
		    } 
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
	}
	
	@SuppressWarnings("rawtypes")
	public static void getDriverInfo(String xm, String sfzh,String ziparea) {
        String methodName = "GetDriverInfo";
        String soapActionURI = namespace+methodName;
        Service service = new Service();
        Call call;
        try {
            call = (Call) service.createCall();
            call.setTargetEndpointAddress(urlStr);
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(soapActionURI);
            call.setOperationName(new QName(namespace, methodName));
            call.addParameter(new QName(namespace, "XM"), XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter(new QName(namespace, "SFZH"), XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter(new QName(namespace, "ziparea"), XMLType.XSD_STRING,ParameterMode.IN);
            call.setReturnType(new QName(namespace, methodName), List.class); // 返回的数据类型
            Object[] str = new Object[3];
            str[0] = xm;
            str[1] = sfzh;
            str[2] =  ziparea;
            List v = (List) call.invoke(str); 
		    for (int i = 0; i < v.size(); i++) { 
		    	
		    } 
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
	}
	
	@SuppressWarnings("rawtypes")
	public static void getOwnerInfo(String yhmc, String xkzh,String ziparea) {
        String methodName = "GetOwnerInfo";
        String soapActionURI = namespace+methodName;
        Service service = new Service();
        Call call;
        try {
            call = (Call) service.createCall();
            call.setTargetEndpointAddress(urlStr);
            call.setUseSOAPAction(true);
            call.setSOAPActionURI(soapActionURI);
            call.setOperationName(new QName(namespace, methodName));
            call.addParameter(new QName(namespace, "YHMC"), XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter(new QName(namespace, "XKZH"), XMLType.XSD_STRING,ParameterMode.IN);
            call.addParameter(new QName(namespace, "ziparea"), XMLType.XSD_STRING,ParameterMode.IN);
            call.setReturnType(new QName(namespace, methodName), List.class); // 返回的数据类型
            Object[] str = new Object[3];
            str[0] = yhmc;
            str[1] = xkzh;
            str[2] = ziparea;
            List v = (List) call.invoke(str); 
		    for (int i = 0; i < v.size(); i++) { 
		    	
		    } 
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
	}
	
	public static Map<String,Object> getCarInfoXml(String cphm, String cpys,String yszhm,String ziparea) throws Exception{
		String soapActionString = "http://tempuri.org/GetCarInfo";
        URL url = new URL(urlStr);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        //拼接请求体,此处也可以在外面写xml文件然后读取,但是为了方便一个文件搞定,而且参数也比较好传递我们直接String拼接(直接将网页上的复制进来即可)
        String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "    <soap:Body>\n" +
                "        <GetCarInfo xmlns=\"http://tempuri.org/\">\n" +
                "            <CPHM>"+cphm+"</CPHM>\n" +
                "            <CPYS>"+cpys+"</CPYS>\n" +
                "            <YSZHM>"+yszhm+"</YSZHM>\n" +
                "            <ziparea>"+ziparea+"</ziparea>\n" +
                "        </GetCarInfo>\n" +
                "    </soap:Body>\n" +
                "</soap:Envelope>";
        byte[] buf = soap.getBytes();
        //设置一些头参数
        httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("soapActionString", soapActionString);
        httpConn.setRequestMethod("POST");
        //输入参数和输出结果
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        OutputStream out = httpConn.getOutputStream();
        out.write(buf);
        out.close();
        
        byte[] datas = readInputStream(httpConn.getInputStream());
        String result = new String(datas);
        ParseXml parse = new ParseXml();
        Map<String,Object> map = parse.parse(result);
        return map;
	}
	
	public static Map<String,Object> getOwnerInfoXml(String yhmc, String xkzh,String ziparea) throws Exception{
		String soapActionString = "http://tempuri.org/GetOwnerInfo";
        URL url = new URL(urlStr);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        //拼接请求体,此处也可以在外面写xml文件然后读取,但是为了方便一个文件搞定,而且参数也比较好传递我们直接String拼接(直接将网页上的复制进来即可)
        String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "    <soap:Body>\n" +
                "        <GetOwnerInfo xmlns=\"http://tempuri.org/\">\n" +
                "            <YHMC>"+yhmc+"</YHMC>\n" +
                "            <XKZH>"+xkzh+"</XKZH>\n" +
                "            <ziparea>"+ziparea+"</ziparea>\n" +
                "        </GetOwnerInfo>\n" +
                "    </soap:Body>\n" +
                "</soap:Envelope>";
        byte[] buf = soap.getBytes();
        //设置一些头参数
        httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("soapActionString", soapActionString);
        httpConn.setRequestMethod("POST");
        //输入参数和输出结果
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        OutputStream out = httpConn.getOutputStream();
        out.write(buf);
        out.close();
        
        byte[] datas = readInputStream(httpConn.getInputStream());
        String result = new String(datas);
        ParseXml parse = new ParseXml();
        Map<String,Object> map = parse.parse(result);
        return map;
	}
	
	public static Map<String,Object> getDriverInfoXml(String xm, String sfzh,String ziparea) throws Exception{
		String soapActionString = "http://tempuri.org/GetDriverInfo";
        URL url = new URL(urlStr);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        //拼接请求体,此处也可以在外面写xml文件然后读取,但是为了方便一个文件搞定,而且参数也比较好传递我们直接String拼接(直接将网页上的复制进来即可)
        String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "    <soap:Body>\n" +
                "        <GetDriverInfo xmlns=\"http://tempuri.org/\">\n" +
                "            <XM>"+xm+"</XM>\n" +
                "            <SFZH>"+sfzh+"</SFZH>\n" +
                "            <ziparea>"+ziparea+"</ziparea>\n" +
                "        </GetDriverInfo>\n" +
                "    </soap:Body>\n" +
                "</soap:Envelope>";
        byte[] buf = soap.getBytes();
        //设置一些头参数
        httpConn.setRequestProperty("Content-Length", String.valueOf(buf.length));
        httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
        httpConn.setRequestProperty("soapActionString", soapActionString);
        httpConn.setRequestMethod("POST");
        //输入参数和输出结果
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        OutputStream out = httpConn.getOutputStream();
        out.write(buf);
        out.close();
       
        byte[] datas = readInputStream(httpConn.getInputStream());
        String result = new String(datas);
        ParseXml parse = new ParseXml();
        Map<String,Object> map = parse.parse(result);
        return map;
	}
	/**
     * 从输入流中读取数据
     *
     * @param inStream
     * @return
     * @throws Exception
     */
    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        inStream.close();
        return data;
    }
}
